package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：
 * @date ：Created in 2019/11/13 0013
 * @description ：
 * @version: 1.0
 */
@Service(interfaceClass = ReportService.class)
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBussinessReport() {
        Map<String, Object> resultData = new HashMap<>();
        //构造今天的日期，将时、分、秒、毫秒都设置为0
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND,0);
        today.set(Calendar.MILLISECOND,0);
        today.setFirstDayOfWeek(Calendar.MONDAY);//设置一周的第一天为星期一
        //本周第一天
        Calendar weekFirsDay = (Calendar) today.clone();
        weekFirsDay.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        //本月第一天
        Calendar monthFirsDay = (Calendar) today.clone();
        monthFirsDay.set(Calendar.DAY_OF_MONTH,1);
        //日期
        resultData.put("reportDate",new SimpleDateFormat("yyyy-MM-dd").format(today.getTime()));
        //会员数据
        // 今日新增会员
        resultData.put("todayNewMember", memberDao.countByRegDateRange(today.getTime(),today.getTime()));
        // 本周新增会员
        resultData.put("thisWeekNewMember", memberDao.countByRegDateRange(weekFirsDay.getTime(),today.getTime()));
        // 本月新增会员
        resultData.put("thisMonthNewMember", memberDao.countByRegDateRange(monthFirsDay.getTime(),today.getTime()));
        //  总会员
        resultData.put("totalMember", memberDao.countByRegTimeBefore(new SimpleDateFormat("yyyy-MM-dd").format(today.getTime())));

        //预约数据
        // 今日预约人数
        resultData.put("todayOrderNumber", orderDao.countByOrderDateRange(today.getTime(),today.getTime(),null));
        // 本周预约人数
        resultData.put("thisWeekOrderNumber", orderDao.countByOrderDateRange(weekFirsDay.getTime(),today.getTime(),null));
        // 本月预约人数
        resultData.put("thisMonthOrderNumber", orderDao.countByOrderDateRange(monthFirsDay.getTime(),today.getTime(),null));

        //到诊数据
        // 今日到诊人数
        resultData.put("todayVisitsNumber", orderDao.countByOrderDateRange(today.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
        // 本周到诊人数
        resultData.put("thisWeekVisitsNumber", orderDao.countByOrderDateRange(weekFirsDay.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
        // 本月到诊人数
        resultData.put("thisMonthVisitsNumber", orderDao.countByOrderDateRange(monthFirsDay.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
        // 热门套餐
        resultData.put("hotSetmeal",orderDao.countByHotSetMeal(4));

        return resultData;
    }
}
